package library.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;
import javax.xml.datatype.DatatypeConfigurationException;

import library.server.Book;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

public class LibraryClient {
	private LibraryClientFrame frame;
	private WebResource res;

	public LibraryClient() {

		try {
			Client client = Client.create();
			res = client
					.resource("http://localhost:8080/libraryREST/resources/Library");

			// WebResource res = client
			// .resource("http://localhost:8080/EsempioREST/resources/message");
			// webTarget.
			// String answer = res.accept("text/plain").get(String.class);
			// System.out.println("Messaggio: " + answer);
			// res.type("text/plain").put("Hello, world");
			// answer = res.accept("text/plain").get(String.class);
			// System.out.println("Nuovo messaggio: " + answer);

			frame = new LibraryClientFrame();
			frame.setVisible(true);
			frame.setSendListener(new SendMessageListener());

		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Connection failed", "Error",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	public static void main(String args[])
			throws DatatypeConfigurationException {
		new LibraryClient();
	}

	private class SendMessageListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					OperationType operation = frame.getMessageType();

					// Creo la richiesta a seconda dell'operazione selezionata
					// sull'interfaccia
					try {
						switch (operation) {
						case DELETE:
							String isdn = frame.getBookISDN();
							if (isdn.equals(""))
								frame.setErrorOnParameters();
							else {

								ClientResponse answer = res.queryParam("isdn",
										isdn).delete(ClientResponse.class);
								//
								System.out.println(answer.getStatus());
								if (answer.getStatus() == 204) {
									frame.addConfirmationText("Book deleted successfully");
								} else {
									frame.addErrorText("Can't delete book. Check the isdn code.");
								}
								// boolean res = libraryServer.deleteBook(isdn);
								// if (res)
								// frame.addConfirmationText("Book deleted successfully");
								// else
								// frame.addErrorText("Can't delete book. Check the isdn code.");
							}
							break;
						case INSERT:
							Book b = frame.getBookParameters();
							if (b == null || b.getISDN().equals(""))
								frame.setErrorOnParameters();
							else {
								ClientResponse answer = res.put(
										ClientResponse.class, b);

								System.out.println(answer.getStatus());
								if (answer.getStatus() == 204) {
									frame.addConfirmationText("Book added successfully");
								} else
									frame.addErrorText("Can't add book. Check the isdn code.");
							}
							break;
						case SELECT:
							frame.setBooks(res.accept("application/xml").get(
									new GenericType<List<Book>>() {
									}));
							break;
						default:
							break;

						}
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null,
								"Connection failed", "Error",
								JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					}

				}

			}).start();
		}
	}
}
